package com.demo.core.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.base.cons.SessionKeyConstant;
import com.demo.base.model.UserInfo;
import com.demo.base.util.CommonUtil;
import com.demo.base.util.Encodes;
import com.demo.core.service.UserInfoService;

@Controller
public class LoginController extends BaseController{

	@Autowired
	private UserInfoService userInfoService;
	
	
	@RequestMapping("/login")
	public @ResponseBody Map<String,Object> login(HttpSession session,@RequestBody UserInfo userInfo){
		
		Map<String,Object> map = new HashMap<String,Object>();
		if(CommonUtil.isNull(userInfo.getLoginName()) || CommonUtil.isNull(userInfo.getPassword())){
			map.put(CommonUtil.ERROR, "用户名或密码不能空！");
			return map;
					}
		String passwordMD5 = Encodes.encodeMd5(userInfo.getPassword());
		userInfo = userInfoService.userAuth(userInfo.getLoginName(), passwordMD5,userInfo.getPassword());
		if (userInfo != null) {
			String sid = this.setSessionObj(SessionKeyConstant.USER_CONTEXT, userInfo);
			map.put("user", userInfo);
			map.put("sid", sid);
		} else {
			map.put(CommonUtil.ERROR, "用户名或密码错误，请检查后再登录！");
		}
		
		return map;
	}
}
